<?php
require_once 'shopping.php';
class Products extends Shopping {

	function __construct() {
		parent::__construct();
		$this->load->model('category');
		$this->load->model('product');

	}

	function get_by_category_id() {
		$category_id = 0;
		if (isset($_POST['category_id'])) {
			$category_id = $_POST['category_id'];
		}

		$temp['products'] = $this->product
				->fetch(array("category_id" => $category_id));
		$this->load->view('template/content', $temp);

		// 	echo $category_id;

	}
	function action_by_id() {
		$action = "";
		if (isset($_POST['action'])) {
			$action = $_POST['action'];
		}
		$id = 0;
		if (isset($_POST['id'])) {
			$id = $_POST['id'];
		}
		if ($action === "delete") {
			$this->product->delete_by_id($id);
		}
		$temp['title'] = 'Shopping-Online';
		$temp['data']['categorys'] = $this->category->fetch();
		$temp['data']['products'] = $this->product->fetch();
		//$temp['view']='backend/backendcontent';
		$this->load->view('backend/backendcontent', $temp);

		// 	echo $category_id;

	}
	function action_add_product() {
		try {
			
				$this->form_validation
						->set_rules('pro_name', "Product's name",
								'required');
				if ($this->form_validation->run() == FALSE) {
							$this->temp['data']['categorys'] = $this->category->load_all();
							$this->load->view('backend/add_product', $this->temp);
				} else {
					$data = array("name" => $this->input->post('pro_name'),
							"pirce" => $this->input->post('pro_pirce'),
							"number" => $this->input->post('pro_number'),
							"hide" => 1,
							"category_id" => $this->input->post('categorys'),
							"detail" => $this->input->post('pro_detail'));
					$this->product->add_product($data);

				}
			

		} catch (Exception $e) {
			echo $e->getMessage();
		}

	}
	function view_add_product() {

		$this->temp['data']['categorys'] = $this->category->load_all();
		$this->temp['view'] = 'backend/add_product';
		$this->load->view('backend/main', $this->temp);
	}

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
